package com.qingxin.sys.verifier;

import java.util.Hashtable;
import java.util.Map;

/**
 * 可锁定Hashtable
 * <br>
 * @author JiWangli
 */
public class LockedHashtable<K, V> extends Hashtable<K, V> {
	private static final long serialVersionUID = -2700327706158303696L;

	private boolean locked;
	
	
	public LockedHashtable() {
		locked = false;
	}
	
	public LockedHashtable(int initialCapacity){
		super(initialCapacity);
		locked = false;
	}

	public LockedHashtable(int initialCapacity, float loadFactor){
		super(initialCapacity, loadFactor);
		locked = false;
	}

	public LockedHashtable(Map<? extends K, ? extends V> map){
		super(map);
		locked = false;
	}

	public synchronized boolean isLocked(){
		return locked;
	}

	public synchronized void setLocked(boolean locked){
		this.locked = locked;
	}

	public synchronized void clear(){
		if(locked){
			throw new IllegalStateException("LockedHashtable对象已锁定,不可修改.");
		} else {
			super.clear();
		}
	}

	public synchronized V put(K key, V value){
		if(locked){
			throw new IllegalStateException("LockedHashtable对象已锁定,不可修改.");
		} else {
			return super.put(key, value);
		}
	}

	public synchronized void putAll(Map<? extends K, ? extends V> map){
		if(locked){
			throw new IllegalStateException("LockedHashtable对象已锁定,不可修改.");
		} else {
			super.putAll(map);
		}
	}

	public synchronized V remove(Object key){
		if(locked){
			throw new IllegalStateException("LockedHashtable对象已锁定,不可修改.");
		} else {
			return super.remove(key);
		}
	}
	
	
	
}
